(function() {

	var app = angular.module('babyApp', []);

	app.controller('SimpleController', [ '$scope', '$http', 'dateFilter', function($scope, $http, dateFilter) {

		delete $http.defaults.headers.common['X-Requested-With'];
		var now = new Date();

		$scope.newLog = {
			"leftBreast" : 15,
			"rightBreast" : 20,
			"pumped" : 30,
			"formula" : 30,
			"diaper" : 'WD',
			"updateUser" : "Amruta [ng]",
			"updateTimestamp" : now
		};
		$scope.logs = [];

		// var localUrl = 'http://localhost:7001/baby-rest-services/log/get/';
		var localUrl = 'http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/get';

		$http.get(localUrl, {
			headers : {
				'Accept' : 'application/json'
			}
		}).success(function(data, status, headers, config) {

			$scope.logs = (data);
		}).error(function(data, status, headers, config) {

			$scope.logs = "error: [" + angular.toJson(status) + "] " + angular.toJson(data);
		});

		// /////////////////////////////////////////////

		// $scope.newLog.timestamp = new Date();

		$scope.$watch('newLog.timestamp', function(timestamp) {

			$scope.timestampString = dateFilter(timestamp, 'yyyy-MM-dd');
		});

		$scope.$watch('timestampString', function(timestampString) {

			if (timestampString) {
				$scope.newLog.timestamp = new Date(timestampString);
			} else {
				$scope.newLog.timestamp = new Date();
			}
		});

		// /////////////////////////////////////////////
		$scope.saveLog = function() {

			// alert('newLog=[' + angular.toJson($scope.newLog) + ']');

			var aNewLog = {
				"timestamp" : 1399067760000,
				"leftBreast" : $scope.newLog.leftBreast,
				"rightBreast" : $scope.newLog.rightBreast,
				"pumped" : $scope.newLog.pumped,
				"formula" : $scope.newLog.formula,
				"diaper" : $scope.newLog.diaper,
				"updateUser" : "Amruta [ng]",
				"updateTimestamp" : 1405979749000
			};

			delete $http.defaults.headers.common['X-Requested-With'];

			// var localAddUrl = 'http://localhost:7001/baby-rest-services/log/add';
			var localAddUrl = 'http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/add';

			$http.post(localAddUrl, angular.toJson($scope.newLog)) //
			.success(function(data, status, headers, config) {

				alert('.. added ..');

			}).error(function(data, status, headers, config) {

				alert('Error [' + status + ']');

			});
		}

	} ]);

})();